extends Node

var client: StreamPeerTCP
var thread: Thread

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

func _exit_tree():
	thread.wait_to_finish()

func _thread_recv(userdata):
	while true:
		var status = client.get_status()
		if status == StreamPeerTCP.STATUS_CONNECTED:
			var num = client.get_available_bytes()
			if num > 0:
				print("recv byte " + num)
				var data = client.get_string(num)
				print(data)
				client.put_string(data)
				continue
		else :
			print("chg exited")
			break;

func chg_connect():
	client = StreamPeerTCP.new()
	client.connect_to_host("127.0.0.1", 20001)
	thread = Thread.new()
	thread.start(_thread_recv, Thread.PRIORITY_LOW)

func chg_disconnect():
	client.disconnect_from_host()
